package com.goethe.utils.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper {
	

	private static final int DB_VERSION = 101;
	
	public  static final String DATABSE_FILE_FOLDER_NAME = "db/";

	private static final String[] FILE_PARTS = { "database.sqlite"};

	private final String DB_PATH;
	private Context context;

//	private static String DB_NAME;

	private SQLiteDatabase myDataBase;

	public DataBaseHelper(Context context) {
		super(context,getDBName(context), null, getVersionNo(context));	
		this.context = context;
		DB_PATH = getDatabasePath(context);
	}
	private static String getDBName(Context context){
		String ret = "";
		try {	
			ret = "lln" + getVersionNo(context) + ".db";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
	private static int getVersionNo(Context context){
//		int ret = 0;
//		try {
//			ret = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return ret;
		return DB_VERSION;
	}

	public void openDataBase() throws SQLException {
		String myPath = DB_PATH + getDBName(context);
		myDataBase = SQLiteDatabase.openDatabase(myPath, null,
				SQLiteDatabase.NO_LOCALIZED_COLLATORS);
	}

	
	public synchronized void close() {
		if (myDataBase != null) {
			myDataBase.close();
		}
		super.close();
	}

	
	public void onCreate(SQLiteDatabase db) {
	}

	
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}

	private static String getDatabasePath(Context context) {
		return "/data/data/" + context.getPackageName() + "/databases/";
	}

	public static void manageDatabase(Context context) {
		try {
			File pFile = new File(getDatabasePath(context));
			if (pFile.exists()) {
				if (pFile.isDirectory()) {
					File[] files = pFile.listFiles();
					if (files != null && files.length > 0) {
						for (int i = 0; i < files.length; i++) {
							if (files[i].isFile()
									&& (!files[i].getName().equals(getDBName(context)))) {
								files[i].delete();
							}
						}
					}
				} else {
					pFile.delete();
					pFile.mkdirs();
				}
			} else {
				pFile.mkdirs();
			}
			File dbFile = new File(pFile, getDBName(context));
			if (!dbFile.exists()) {
				OutputStream myOutput = new FileOutputStream(dbFile);
				byte[] buffer = new byte[1024];
				int length;
				for (int i = 0; i < FILE_PARTS.length; i++) {
					InputStream myInput = context.getAssets().open(DATABSE_FILE_FOLDER_NAME+
							FILE_PARTS[i]);
					while ((length = myInput.read(buffer)) > 0) {
						myOutput.write(buffer, 0, length);
					}
					myInput.close();
				}
				myOutput.flush();
				myOutput.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
